#!/bin/bash

# from: https://gist.github.com/bahamat/b1f6511ebdaea510a0f9391c75e2362c
# Script to update/upgrade all components of SDC/Triton


set -o errexit
set -o xtrace

imgadm vacuum -f

sdcadm update --all --just-images -y
sdcadm self-update --allow-major-update --latest
sdcadm dc-maint start && printf "DC maintenance has started at %s\n" "$(date)"
sdcadm update --all -y
sdcadm experimental update-agents --latest --all -y
sdcadm experimental update-other
sdcadm experimental update-gz-tools --latest
sdcadm dc-maint stop && printf "DC maintenance has ended at %s\n" "$(date)"
sdcadm platform remove -a --cleanup-cache -y
sdcadm platform install --latest
sdcadm platform assign --latest --all
sdcadm platform set-default --latest

sdc-oneachnode -a 'sdc-usbkey update'

# Optional, will disrupt fabric network traffic
sdcadm update portolan --force-data-path

printf 'Update of all components complete\n'

now=$(date +%Y%m%dT%H%M%SZ)
sdc-vmapi /vms?state=active\&tag.smartdc_type=core | json -Ha > "/opt/custom/core_services_${now}.json"

exit

    update-agents       Update SDC agents
    dc-maint            Show and modify the DC maintenance mode.
    update-other        Temporary grabbag for small SDC update steps.
    update-gz-tools     Temporary grabbag for updating the SDC global zone tools.
    add-new-agent-svcs  Temporary grabbag for installing the SDC global zone new agents.
    update-docker       Add/update the docker service.
    install-docker-cert  Installs a custom TLS certificate to be used by sdc-docker.
    portolan            Add/update the portolan service.
    fabrics             Initialize fabrics in the datacenter.
    default-fabric      Initialize a default fabric for an account.